require 'digest/sha1'

# this model expects a certain database layout and its based on the name/login pattern. 
class Account < ActiveRecord::Base
    
    belongs_to :user
    
    attr_accessor :password_confirmation
    
    def self.authenticate(login, pass)
        find_first(["login = ? AND password = ?", login, sha1(pass)])
    end  
    
    def change_password(pass)
        update_attribute "password", self.class.sha1(pass)
    end
    
    def sites
        user = User.find_first(["username = ?", self.login])
        return user.sites 
    end
    
    protected
    
    def self.sha1(pass)
        Digest::SHA1.hexdigest("change-me--#{pass}--")
    end
    
    before_create :crypt_password
    
    def crypt_password
        write_attribute("password", self.class.sha1(password))
    end
    
end
